import useSWR from 'swr';
import { fetchMiner } from '@/service/miner';
import { GroupPageModel } from '@/service/group';
import { MonitorPageModel } from '@/types/monitor';

export interface FetchMinerParams {
  status: number;
  page: number;
  limit: number;
  order: string;
  mid?: number;
  gid?: number;
  search: string;
}

export function useFetchMinerSwr(
  mid: number,
  groupModel: GroupPageModel | undefined,
  monitorInfo: MonitorPageModel | undefined,
  gid: number | undefined,
  fetchMinerParams: FetchMinerParams,
  isAutoReFresh = false,
) {
  return useSWR(
    mid && groupModel && monitorInfo
      ? [
          `/miner`,
          mid,
          gid,
          fetchMinerParams.status,
          fetchMinerParams.page,
          fetchMinerParams.limit,
          fetchMinerParams.order,
          fetchMinerParams.search,
          monitorInfo?.info?.temperature1,
          monitorInfo?.info?.temperature2,
        ]
      : null,
    (url, mid, gid, status, page, limit, order, search, t1, t2) => {
      return fetchMiner(
        {
          status,
          page,
          limit,
          order,
          mid,
          gid,
          search,
        },
        t1,
        t2,
        groupModel?.groupListMap,
      );
    },
    {
      refreshInterval: isAutoReFresh ? 15 * 1000 : 0,
    },
  );
}
